﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WeatherSystem.Data
{
    class Convert
    {
        public static long ToTimeStamp(System.DateTime time)
        {
            DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            return (long)(time.AddHours(-8) - Jan1st1970).TotalSeconds;
        }

        public static DateTime ToDateTime(long timestamp10)
        {
            var nowTime = new DateTime(1970, 1, 1, 8, 0, 0).AddSeconds(timestamp10);
            return nowTime;
        }

        public static string WeatherDataToString(Dictionary<string,object> data)
        {
            var timestamp = data["time"].ToString();
            var longitude = data["longitude"].ToString();
            var latitude = data["latitude"].ToString();
            var altitude = data["altitude"].ToString();
            var temp = data["temp"].ToString();
            var wet = data["wet"].ToString();
            var press = data["press"].ToString();
            var sb = new StringBuilder();
            sb.AppendFormat("测量时间：{0}" + Environment.NewLine, ToDateTime(System.Convert.ToInt64(timestamp)));
            sb.AppendFormat("温度：{0}℃" + Environment.NewLine, temp);
            sb.AppendFormat("湿度：{0}%RH" + Environment.NewLine, wet);
            sb.AppendFormat("压强：{0}MPa" + Environment.NewLine, press);
            sb.AppendFormat("海拔：{0}m" + Environment.NewLine, altitude);
            sb.AppendFormat("经度：{0}" + Environment.NewLine, longitude);
            sb.AppendFormat("纬度：{0}" + Environment.NewLine, latitude);
            return sb.ToString();
        }
    }
}
